import 'package:flutter/material.dart';
import 'package:pcms/models/status_models.dart';
import 'package:pcms/services/mock_data_service.dart';

class AirSourceUnitStatusWidget extends StatelessWidget {
  const AirSourceUnitStatusWidget({super.key});

  @override
  Widget build(BuildContext context) {
    final List<AirSourceUnitInfo> units = MockDataService.getAirSourceUnitInfo();

    return LayoutBuilder(
      builder: (context, constraints) {
        final itemWidth = (constraints.maxWidth - 32) / 5; // 5 items per row
        return Wrap(
          spacing: 4.0,
          runSpacing: 4.0,
          children: units.map((unit) {
            // Remove DB5.DB prefix and format as requested
            final addressParts = unit.address.split('.');
            final shortAddress = addressParts.length > 1 ? addressParts.sublist(1).join('.') : unit.address;
            return SizedBox(
              width: itemWidth,
              child: Container(
                margin: const EdgeInsets.symmetric(horizontal: 2, vertical: 2),
                child: Container(
                  decoration: BoxDecoration(
                    color: const Color(0xFF0B1A3E),
                    borderRadius: BorderRadius.circular(4),
                    border: Border.all(
                      color: unit.status == 1 ? const Color(0xFF0A79DF).withOpacity(0.3) : Colors.red.withOpacity(0.3),
                      width: 1,
                    ),
                  ),
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          width: 8,
                          height: 8,
                          decoration: BoxDecoration(
                            color: unit.status == 1 ? const Color(0xFF0A79DF) : Colors.red,
                            borderRadius: BorderRadius.circular(4),
                            boxShadow: [
                              BoxShadow(
                                color: (unit.status == 1 ? const Color(0xFF0A79DF) : Colors.red).withOpacity(0.3),
                                blurRadius: 6,
                                spreadRadius: 2,
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(width: 8),
                        Flexible(
                          child: Text(
                            '${unit.label}($shortAddress)',
                            style: TextStyle(
                              color: unit.status == 1 ? Colors.white : Colors.white70,
                              fontSize: 12,
                            ),
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            );
          }).toList(),
        );
      },
    );
  }
}